package com.le.tester.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * createTime：2021/12/10 14:41
 * description：无重复字符的最长字串,使用滑动窗口位移方式实现
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {

        int result = 0;
        int curStrLength = s.length();

        Map<Character, Integer> map = new HashMap<>();

        for (int end = 0, start = 0; end < curStrLength; end++) {
            char alpha = s.charAt(end);
            if (map.containsKey(alpha)) {
                start = Math.max(map.get(alpha), start);
            }
            result = Math.max(result, end - start + 1);
            map.put(s.charAt(end), end + 1);

        }
        return result;
    }

    public static void main(String[] args) {
        int testOne = new LengthOfLongestSubstring().lengthOfLongestSubstring("abcabcbb");
        System.out.println(testOne);
    }
}
